package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.pojo.Dynamic;
import com.aistar.service.DynamicPraiseService;
import com.aistar.service.DynamicService;
import com.aistar.serviceVO.DynamicAllVOService;
import com.aistar.serviceVO.DynamicCommentVOService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/dynamic")
public class DynamicController {
    private DynamicService dynamicService;
    private DynamicAllVOService dynamicAllVOService;
    private DynamicCommentVOService dynamicCommentVOService;
    private DynamicPraiseService dynamicPraiseService;
//    private DynamicCollectService dynamicCollectService;

    @Autowired
    public DynamicController(DynamicService dynamicService,
                             DynamicAllVOService dynamicAllVOService,
                             DynamicCommentVOService dynamicCommentVOService,
                             DynamicPraiseService dynamicPraiseService) {
        this.dynamicService = dynamicService;
        this.dynamicAllVOService = dynamicAllVOService;
        this.dynamicCommentVOService = dynamicCommentVOService;
        this.dynamicPraiseService = dynamicPraiseService;
    }

    @RequestMapping
    public ModelAndView getByPageForIndex(Integer page) {
        page = page == null ? 1 : page;
        ModelAndView mav = new ModelAndView();
        mav.addObject("pageInfo", dynamicAllVOService.getByPageOrderByDynamicTime(page, 10).getData());
        mav.addObject("hot", dynamicService.getByPage(1, 10).getData());
        mav.setViewName("dynamic/dynamic");
        return mav;
    }

    @RequestMapping("/create")
    public String create() {
        return "dynamic/dynamicCreate";
    }

    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody Dynamic dynamic, HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        int customerId = customer.getCustomerId();
        dynamic.setDynamicTime(new Date());
        dynamic.setCustomerId(customerId);
        return dynamicService.add(dynamic);
    }

    @RequestMapping("{id}")
    public ModelAndView detail(@PathVariable("id") Integer dynamicId, HttpServletRequest request) {
        Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        ModelAndView mav = new ModelAndView();
        mav.addObject("hot", dynamicService.getByPage(1, 10).getData());
        mav.addObject("dynamicAllVO", dynamicAllVOService.getByDynamicId(dynamicId).getData());
        if (customer != null)
            mav.addObject("ssdPraise", dynamicPraiseService.getByCidAndPid(customer.getCustomerId(), dynamicId));
//        mav.addObject("isCollect", dynamicCollectService.getByCidAndPid(customer.getCustomerId(), dynamicId).getData());

        ServerResponse serverResponse = dynamicCommentVOService.getByFatherDynamicId(dynamicId);
        if (serverResponse.getStatus() == 1)
            mav.addObject("commentList", serverResponse.getData());

        mav.setViewName("dynamic/dynamicDetail");
        return mav;
    }

}
